package main;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.JButton;

import java.awt.Dimension;

import javax.swing.JCheckBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;

import main.BibFile.TypeOfField;


@SuppressWarnings("serial")
public class URL4BibTeX extends javax.swing.JFrame {

	private JButton cmdRefact;
	private JButton cmdSearch;
	private JLabel lblFile;
	private JTextField txtFile;
	private JTextField txtStringDate;
	private JCheckBox chkDefaultFile;
	private JLabel lblString;
	private JComboBox<String> cbmField;
	private JLabel lblStringURL;
	private JTextField txtStringURL;
	private JCheckBox chkMISCTypes; 
	
	  
    public URL4BibTeX() {
    	super.setSize(200, 130);
    	setResizable(false);
    	setMaximumSize(new Dimension(400, 130));
    	setMinimumSize(new Dimension(400, 130));
    	setType(Type.UTILITY);
    	setAutoRequestFocus(false);
    	setLocationRelativeTo(null);
      
    	initComponents();
    	this.setVisible(true);
    }
    
    
    private void initComponents() {

        cmdSearch = new javax.swing.JButton();
        lblFile = new javax.swing.JLabel();
        txtFile = new javax.swing.JTextField();
        
        //txtFile.setText("D:\\Documents\\library.bib");
       
        cmdRefact = new javax.swing.JButton();
        cmdRefact.addMouseListener(new MouseAdapter() {
        	 public void mouseClicked(java.awt.event.MouseEvent evt) {
                 cmdRefact_click();
             }
         });
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        

        cmdSearch.setText("Search"); //$NON-NLS-1$
        cmdSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cmdSearch_click();
            }
        });

        lblFile.setText("File"); //$NON-NLS-1$

        cmdRefact.setText("Refact"); //$NON-NLS-1$
        
        JButton txtCancelar = new JButton("Cancel"); //$NON-NLS-1$
        txtCancelar.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		System.exit(DISPOSE_ON_CLOSE);
        	}
        });
        
        chkDefaultFile = new JCheckBox("Load default file");
        
        chkDefaultFile.setSelected(false);
        
        chkDefaultFile.addMouseListener(new MouseAdapter() {
        	public void mouseClicked(MouseEvent e) {
        		chkDefaultFile_click();
        	}
        });
        
        cbmField = new JComboBox<String>();
        cbmField.setModel(new DefaultComboBoxModel(TypeOfField.values()));
        cbmField.setSelectedIndex(0);
        
        JLabel lblField = new JLabel("Generating in field:");
        
        txtStringDate = new JTextField();
        txtStringDate.setText("Consultado el");
        txtStringDate.setColumns(10);
        
        lblString = new JLabel("String for Date (if exists)");
        
        lblStringURL = new JLabel("String for URL");
        
        txtStringURL = new JTextField();
        txtStringURL.setText("Disponible en");
        txtStringURL.setColumns(10);
        
        chkMISCTypes = new JCheckBox("Only for @MISC and @\r\ntechreport types");
        chkMISCTypes.setSelected(true);
        
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(Alignment.LEADING, layout.createSequentialGroup()
        			.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        				.addGroup(layout.createSequentialGroup()
        					.addContainerGap()
        					.addComponent(txtCancelar)
        					.addGap(18)
        					.addComponent(cmdRefact))
        				.addGroup(Alignment.LEADING, layout.createSequentialGroup()
        					.addGap(27)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addComponent(chkDefaultFile)
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(lblFile)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(txtFile, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE))
        						.addGroup(layout.createSequentialGroup()
        							.addGap(34)
        							.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        								.addComponent(lblField)
        								.addComponent(lblString)
        								.addComponent(lblStringURL))
        							.addPreferredGap(ComponentPlacement.UNRELATED)
        							.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
        								.addComponent(cbmField, Alignment.LEADING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        								.addComponent(txtStringURL, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
        								.addComponent(txtStringDate, Alignment.LEADING)
        								.addComponent(cmdSearch, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)))
        						.addComponent(chkMISCTypes, Alignment.TRAILING))))
        			.addContainerGap(61, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addGap(28)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblFile)
        				.addComponent(txtFile, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(cmdSearch))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(chkDefaultFile)
        			.addGap(13)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(cbmField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblField))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(txtStringURL, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblStringURL))
        			.addGap(11)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(txtStringDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblString))
        			.addGap(8)
        			.addComponent(chkMISCTypes)
        			.addGap(18)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(cmdRefact)
        				.addComponent(txtCancelar))
        			.addContainerGap())
        );
        getContentPane().setLayout(layout);
        
        this.setTitle(Messages.getString("URL4BibTeX.Constants.appName") + " " + Messages.getString("URL4BibTeX.Constants.appVer"));   //$NON-NLS-1$

        pack();
    }

    private void cmdRefact_click(){
    	// Chequeamos que existe el fichero
		if (("").equals(txtFile.getText())){ 
			Messages.showErrorMessage("URL4BibTeX.UI.selectFile"); 
		}else{
			BibFile bibFile = new BibFile();
			bibFile.URL4BibTeX(txtFile.getText(), cbmField.getSelectedItem().toString(), txtStringURL.getText(), txtStringDate.getText(), chkMISCTypes.isSelected());
		
			Messages.showInfoMessage(Messages.getString("URL4BibTeX.UI.conversionOK"));
		
			System.exit(DISPOSE_ON_CLOSE);
		}
    }

    private void cmdSearch_click() {
        
    	JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
        		Messages.getString("URL4BibTeX.Constants.messageTypeFiles"), 
        		Messages.getString("URL4BibTeX.Constants.extensionTypeFiles")); 
        
        chooser.setFileFilter(filter);
        
        int returnVal = chooser.showOpenDialog(this);
        
        if(returnVal == JFileChooser.APPROVE_OPTION) {
        	txtFile.setText(chooser.getSelectedFile().getAbsolutePath());  
        
        }
    }
        
     private void chkDefaultFile_click(){

 		if (chkDefaultFile.isSelected()){
 			txtFile.setText(Messages.getString("URL4BibTeX.Constants.defaultFile"));
 			txtFile.setEnabled(false);
 			cmdSearch.setEnabled(false);
 			
 		}else{
 			txtFile.setText("");
 			txtFile.setEnabled(true);
 			cmdSearch.setEnabled(true);
 			
 		}
     }

    
    public static void main(String args[]) {
        
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) { //$NON-NLS-1$
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(URL4BibTeX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(URL4BibTeX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(URL4BibTeX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(URL4BibTeX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        

        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new URL4BibTeX().setVisible(true);
            }
        });
    }
}

